package com.yimi.yinhepda.views.createPackage_module;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import butterknife.Bind;

import com.yimi.yinhepda.R;
import com.yimi.yinhepda.adapter.BuildPackageTempSaveAdapter;
import com.yimi.yinhepda.db.dao.BuildPckScanTableDao;
import com.yimi.yinhepda.db.table.BuildPckScanTable;
import com.yimi.yinhepda.entity.bean.BuildPackageTmepSaveEntity;
import com.yimi.yinhepda.utils.Common;
import com.pda.ym.utils.Constants;
import com.yimi.yinhepda.utils.IntentUtils;
import com.pda.uce.commom.util.MyDialog;
import com.yimi.yinhepda.base.BaseInitActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 建包任务暂存集
 *
 * @author 000432
 * @date 2018/1/26 0026
 */

public class CreatePackageTaskTempSaveList extends BaseInitActivity {

    /**
     * 标题栏左边
     */
    @Bind(R.id.title)
    TextView mTvTitle;
    /**
     * 标题栏中间
     */
    @Bind(R.id.num)
    TextView mTvNum;
    /**
     * 任务暂存列表
     */
    @Bind(R.id.create_package_task_list)
    ListView mTaskListView;
    /**
     * 返回
     */
    @Bind(R.id.create_package_temp_back)
    Button mBtnBack;
    /**
     * 删除
     */
    @Bind(R.id.create_package_temp_delete)
    Button mBtnDelete;
    /**
     * 执行
     */
    @Bind(R.id.create_package_temp_cexc)
    Button mBtnExec;
    private BuildPackageTempSaveAdapter mTempSaveAdapter;
    private BuildPckScanTableDao buildPckScanTableDao;
    private List<BuildPackageTmepSaveEntity> listTaskEntity;

    @Override
    public String getPageName() {
        return "建包任务暂存界面";
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_create_package_task_temp_save_list;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        mTvTitle.setText("任务暂存集");
    }

    @Override
    public void initEvent() {
        mBtnBack.setOnClickListener(this);
        mBtnDelete.setOnClickListener(this);
        mBtnExec.setOnClickListener(this);
    }

    @Override
    public void initData() {
        buildPckScanTableDao = new BuildPckScanTableDao();
        listTaskEntity = new ArrayList<>();

        //查询暂存的运单号
        List<BuildPckScanTable> listUnloadTaskTable = buildPckScanTableDao.findUnComplete(0, sharedPreferUtil.getValue(Common.USER_CODE));
        if (listUnloadTaskTable != null && listUnloadTaskTable.size() > 0) {
            //获取任务号
            Map<String, String> map = new HashMap<>();
            for (BuildPckScanTable buildPckScanTable : listUnloadTaskTable) {
                map.put(buildPckScanTable.getTaskNo(), buildPckScanTable.getTaskNo());
            }
            for (String taskNo : map.values()) {
                String packageNo = "";
                String destDeptName = "";
                String destDeptCode = "";
                Map<String, String> mapPriceNum = new HashMap<>();
                //件数
                int pieceNum = 0;
                for (BuildPckScanTable item : listUnloadTaskTable) {
                    if (item.getTaskNo().equals(taskNo)) {
                        packageNo = item.getPackageNo();
                        destDeptName = item.getDestDeptName();
                        destDeptCode = item.getDestDeptCode();
                        pieceNum++;
                        //票数
                        mapPriceNum.put(item.getWaybillNo(), item.getWaybillNo());
                    }
                }
                listTaskEntity.add(new BuildPackageTmepSaveEntity(taskNo, packageNo, destDeptName, destDeptCode, mapPriceNum.size(), pieceNum, false));
            }
        }

        if (listTaskEntity != null && listTaskEntity.size() > 0) {
            mTempSaveAdapter = new BuildPackageTempSaveAdapter(this, listTaskEntity);
            mTaskListView.setAdapter(mTempSaveAdapter);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        /** 查询未上传数据*/
        int totalNotUploadNum = new BuildPckScanTableDao().queryNotUploadDataNum();
        mTvNum.setText(totalNotUploadNum + "");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.create_package_temp_back:
                finish();
                break;
            case R.id.create_package_temp_delete:
                //删除按钮
                if (listTaskEntity != null && listTaskEntity.size() > 0) {
                    final int i = mTempSaveAdapter.getCheck();
                    if (i == -1) {
                        showToast("请先选中要删除的项！");
                        return;
                    }
                    MyDialog backDialog = new MyDialog(this);
                    backDialog.setIcon(R.drawable.alert);
                    backDialog.setMessage("确定删除该任务？");
                    backDialog.setPositiveClickListener( Constants.confirmMsg, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            BuildPackageTmepSaveEntity entity = listTaskEntity.get(i);
                            //根据任务号删除数据
                            new BuildPckScanTableDao().deleteDataByTaskNo(entity.getTaskNo());
                            //移除列表中的这条数据
                            listTaskEntity.remove(i);
                            mTempSaveAdapter.notifyDataSetChanged();
                            dialog.dismiss();
                            onResume();
                        }
                    });
                    backDialog.setNegativeClickListener(R.drawable.btn_cancel_selector, Constants.cancelMsg, null);
                    backDialog.show();
                }
                break;
            case R.id.create_package_temp_cexc:
                //执行按钮
                if (listTaskEntity != null && listTaskEntity.size() > 0) {
                    int i = mTempSaveAdapter.getCheck();
                    if (i == -1) {
                        showToast("请先选中要执行的项！");
                        return;
                    }
                    BuildPackageTmepSaveEntity entity = listTaskEntity.get(i);
                    Intent intent = new Intent(this, CreatePackageScanActivity.class);
                    intent.putExtra(IntentUtils.BUILD_PKG_TASK_NO, entity.getTaskNo());
                    intent.putExtra(IntentUtils.BUILD_DEST, entity.getDestDeptCode());
                    intent.putExtra(IntentUtils.BUILD_DEST_NAME, entity.getDestDeptName());
                    startActivity(intent);
                    finish();
                }
                break;
            default:
                break;
        }
    }
}
